﻿using Common;
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoManageSys.Areas.Filters;
using Models.DTO;
using Microsoft.AspNetCore.Mvc;
using Models;
using BLL;

namespace DemoManageSys.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class ShopInfoController : Controller
    {
        private IShopInfoBLL _shopInfoBLL;

        public ShopInfoController(
            IShopInfoBLL shopInfoBLL
            )
        {
            _shopInfoBLL = shopInfoBLL;
        }

        #region 显示视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateView()
        {
            return View();
        }

        public IActionResult EditView()
        {
            return View();
        }
        #endregion

        #region 获取的方法 (GetShopInfoes)
        [HttpGet]
        public IActionResult GetShopInfo(int page, int limit, string customerName)
        {
            int count;
            //调用BLL的查询方法
            List<GetShopInfoDTO> getShopInfoDTOs = _shopInfoBLL.GetShopInfo(page, limit, customerName, out count);

            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getShopInfoDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion ()

        #region 添加的方法 (CreateShopInfo)
        [HttpPost]
        public IActionResult CreateShopInfo([FromForm] ShopInfo shop)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _shopInfoBLL.CreateShopInfo(shop, out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
                resule.Msg = msg;
            }
            else
            {
                resule.Msg = msg;

            }
            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeleteShopInfo)
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteShopInfo(string id)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "删除用户的id不能为空";
                return Json(resule);
            }

            resule.IsSuccess = _shopInfoBLL.DeleteShopInfo(id);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Msg = "删除用户成功";
                resule.Code = 200;
            }
            else
            {
                resule.Msg = "删除用户失败";
            }
            return Json(resule);
        }
        #endregion

        #region 批量软删除用户的方法 (DeleteListShopInfo)
        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteListShopInfo(List<string> ids)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (ids.Count == 0)
            {
                resule.Msg = "你没有返回任何用户";
                return Json(resule);
            }
            bool isOK = _shopInfoBLL.DeleteListShopInfo(ids);//调用方法传值
            resule.Code = 200;
            resule.IsSuccess = isOK;
            resule.Msg = "删除用户成功";
            return Json(resule);
        }
        #endregion

        #region 修改的方法 (UpdateShopInfo)
        /// <summary>
        /// 修改的方法
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateShopInfo([FromForm] ShopInfo shop)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _shopInfoBLL.UpdateShopInfo(shop, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion

        #region 根据ID获取下拉框 (GetShopInfoById)
        [HttpGet]
        public IActionResult GetShopInfoById(string id)
        {
            ReturnResule resule = new ReturnResule();

            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "部门ID为空";
                return Json(resule);
            }

            ShopInfo shop = _shopInfoBLL.GetShopInfoById(id);
            if(shop == null)
            {
                resule.Msg = "未获取到部门信息";
            }
            else
            {
                var selectOption = _shopInfoBLL.GetSelectOption();
                resule.Msg = "成功";
                resule.Code = 200;
                resule.Data = new {
                    shop = shop,//部门信息
                    SelectOption = selectOption//下拉列表数据
                };
            }
            return Json(resule);
        }
        #endregion

        #region 获取下拉框的方法 (GetSelectOption)
        /// <summary>
        /// 获取下拉框的方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSelectOption()
        {
            ReturnResule resule = new ReturnResule();
            var data = _shopInfoBLL.GetSelectOption();

            if (data != null)
            {
                resule.Msg = "返回成功";
                resule.Data = data;
                resule.Code = 200;
            }
            return Json(resule);
        }
        #endregion
    }
}